<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* Name:  Teams Model
*
* Author: John Schop, john.schop@gmail.com
*
*/

class Teams_model extends CI_Model
{
	

	public function __construct()
	{
		parent::__construct();
		$this->load->database();
	}
	

	/**
	 * Get all teams
	 *
	 * @access public
	 * @return object
	 */
	function get_all()
	{
		$this->db->order_by('teams.team_number', 'asc');
		return $this->db->get('teams')->result();
	}

  /**
   * Get team by id
   *
   * @access public
   * @param string $team_id
   * @return object team object
   */
  function get_by_id($team_id)
  {
      return $this->db->get_where('teams', array('team_id' => $team_id))->row();
  }

  function get_by_number($team_number)
  {
      return $this->db->get_where('teams', array('team_number' => $team_number))->row();
  }

  function update($team_number, $attributes = array())
  {

      // Update
      if ($this->get_by_number($team_number))
      {
          $this->db->where('team_number', $team_number);
          $this->db->update('teams', $attributes);
      }
      // Insert
      else
      {
          $attributes['team_number'] = $team_number;
          $this->db->insert('teams', $attributes);
      }

  }


}
